//
//  Toast.m
//  DSA_Broker
//
//  Created by LIKUN on 15-4-15.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "JJRToast.h"

@implementation JJRToast

- (instancetype)initWithFrame:(CGRect)frame Message:(NSString *)msg superV:(UIView *)view
{
    self = [super initWithFrame:frame];
    if (self) {
        
        if ([msg isEqualToString:@""]||msg == nil) {
            return nil;
        }
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        for (UIView *view in [window subviews]) {
            if ([view isKindOfClass:[JJRToast class]]) {
                [view removeFromSuperview];
            }
        }
        
        UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
        
        UIView *toastView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        toastView.backgroundColor = [UIColor darkGrayColor];
        [self addSubview:toastView];
        
        UILabel *msgLbl = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, frame.size.width-20*2, frame.size.height-5*2)];
        msgLbl.backgroundColor = [UIColor clearColor];
        msgLbl.textAlignment = 1;
        msgLbl.text = msg;
        msgLbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0];
        msgLbl.textColor = [UIColor whiteColor];
        msgLbl.numberOfLines = 0;
        [toastView addSubview:msgLbl];
        
        NSDictionary *attribute = @{NSFontAttributeName:font};
        CGSize retSize = [msg boundingRectWithSize:CGSizeMake(frame.size.width-20*2, 0)
                                                 options:
                          NSStringDrawingUsesLineFragmentOrigin |
                          NSStringDrawingUsesFontLeading 
                                              attributes:attribute 
                                                 context:nil].size;
        
        
        CGRect lblFrame = msgLbl.frame;
        lblFrame.size.height = retSize.height;
        msgLbl.frame = lblFrame;
        
        CGRect toastFrame = self.frame;
        toastFrame.size.height = retSize.height+5*2;
        
        if (toastFrame.size.height<60) {
            toastFrame.size.height = 60;
            float _y =(toastFrame.size.height-lblFrame.size.height)/2;
            lblFrame.origin.y = _y;
            msgLbl.frame = lblFrame;
        }

//        toastFrame.origin.y = ScreenHeight-toastFrame.size.height;
        toastFrame.origin.y = view.frame.size.height-toastFrame.size.height;

        self.frame = toastFrame;

        [toastView setFrame:CGRectMake(0, 0, toastFrame.size.width, toastFrame.size.height)];
        [self performSelectorOnMainThread:@selector(delayRemoveToast) withObject:nil waitUntilDone:NO];
    }
    return self;
}
- (void)delayRemoveToast
{
    [self performSelector:@selector(removeToast) withObject:nil afterDelay:2.0];
}
- (void)removeToast
{
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
@end
